﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BEGDC.PreContract.BLL;
using BEGDC.Utility.Castle;

namespace PreContractWeb.Task
{
    public partial class TransferredPreContract : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                DateTime now = DateTime.Now;
                this.tbSearchStartDate.Text = new DateTime(now.Year, now.Month, 1).ToShortDateString();
                this.tbSearchEndDate.Text = now.ToShortDateString();

                this.BindGridView();
            }
        }

        private void BindGridView()
        {
            IPreContractInfo iPreContractInfo = (IPreContractInfo)AppContainer.Instance[Services.CONST_PreContractInfo_Service];
            DataTable dt = iPreContractInfo.GetTransferredPreContract(this.tbPreContractNo.Text.Trim(), this.dpContractStatus.SelectedValue, this.tbSearchStartDate.Text.Trim(), this.tbSearchEndDate.Text.Trim());
            this.gvTransferredPreContract.DataSource = dt.DefaultView;
            this.gvTransferredPreContract.DataBind();

            this.totalSpan.InnerText = dt.DefaultView.Count.ToString();
            this.pgTransferredPreContract.Visible = true;
            this.pgTransferredPreContract.BindtoGridView(this.gvTransferredPreContract, dt.DefaultView);
            if (dt.Rows.Count == 0)
            {
                this.pgTransferredPreContract.Visible = false;
            }
        }

        protected void btnQuickSearch_Click(object sender, EventArgs e)
        {
            if (CheckValues())
            {
                this.pgTransferredPreContract.GVCurrentPageIndex = 1;
                this.BindGridView();
            }
        }

        protected void btnQuickExport_Click(object sender, EventArgs e)
        {
            if (CheckValues())
            {  
                this.gvTransferredPreContract.PageSize = Convert.ToInt32(this.totalSpan.InnerText);
                this.BindGridView();
                DataView dt = (DataView)this.gvTransferredPreContract.DataSource;
                if (dt != null && dt.Count > 0)
                {
                    BaseFunction.ExportToExcel(Page, this.gvTransferredPreContract, "Pre-ContractList.xls");
                }
                else
                {
                    ClientScript.RegisterStartupScript(GetType(), "", "<script>alert('没有查询结果导出！\\r\\nNo data to be exported');</script>");
                }
            }
        }

        protected void gvTransferredPreContract_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowIndex >= 0)
            {
                e.Row.Cells[6].Text = "<a onclick=\"javascript:ShowPage('" + e.Row.Cells[0].Text.Trim() + "');\">" + e.Row.Cells[6].Text.Trim() + "</a>";
            }
        }

        private bool CheckValues()
        {
            DateTime dt;
            if (!DateTime.TryParse(this.tbSearchStartDate.Text.Trim(), out dt))
            {
                this.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('" + "转正起始时间输入有误." + "')</script>");
                return false;
            }
            if (!DateTime.TryParse(this.tbSearchEndDate.Text.Trim(), out dt))
            {
                this.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('" + "转正截止时间输入有误." + "')</script>");
                return false;
            }
            return true;
        }
    }
}